<html>
<head><meta charset="utf-8"><title>Getting serde derive deserialize to capture unknown enum ... · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Getting.20serde.20derive.20deserialize.20to.20capture.20unknown.20enum.20.2E.2E.2E.html">Getting serde derive deserialize to capture unknown enum ...</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243414776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Getting%20serde%20derive%20deserialize%20to%20capture%20unknown%20enum%20.../near/243414776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David McGillicuddy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Getting.20serde.20derive.20deserialize.20to.20capture.20unknown.20enum.20.2E.2E.2E.html#243414776">(Jun 21 2021 at 17:24)</a>:</h4>
<p>Hi, I've got another question along the lines of "how do I trick serde derive into doing this quite specific non-standard thing?".<br>
If I have an enum along the lines of <code>enum Foo { VariantA }</code>, container <code>struct FooVariants { #[serde(flatten)] inner: HashMap&lt;Foo, bool&gt; }</code> and json:</p>
<div class="codehilite"><pre><span></span><code>{&quot;variantA&quot;: true, &quot;variantB&quot;, false}
</code></pre></div>
<p>I'd like to be able to get the valid values, but also be told what invalid variants there were. So some result like:</p>
<div class="codehilite"><pre><span></span><code>FooVariants {
    inner: hashmap![(Foo::VariantA, true)],
    other: hashmap![(&quot;variantB&quot;, false)]
}
</code></pre></div>
<p>or</p>
<div class="codehilite"><pre><span></span><code>FooVariants { inner: hashmap![
    (Foo::VariantA, true),
    (Foo::Other(&quot;variantB&quot;), false)
]  }
</code></pre></div>
<p>I don't mind reformatting these data structures around a bit as required. Currently I have:</p>
<div class="codehilite"><pre><span></span><code>struct FooVariants {
    #[serde(flatten)]
    inner: HashMap&lt;Foo, bool&gt;,

    #[serde(flatten)]
    other: HashMap&lt;String, bool&gt;
 }
</code></pre></div>
<p>but this results in:</p>
<div class="codehilite"><pre><span></span><code>FooVariants {
    inner: hashmap![(Foo::VariantA, true)],
    other: hashmap![(&quot;variantA&quot;, true), (&quot;variantB&quot;, false)]
}
</code></pre></div>
<p>I think because <code>#[serde(flatten)]</code> is not supposed to be used for both making an inner hashmap transparent _and_ for collecting unparsed fields. Sorry for the rambling question, happy to accept any suggestions for how I can make this kind of thing work. I suppose just manually removing <code>inner</code>'s fields from <code>other</code> would work, but then I'm going back from the enum to the string again which seems less than ideal.</p>



<a name="243420536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Getting%20serde%20derive%20deserialize%20to%20capture%20unknown%20enum%20.../near/243420536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Getting.20serde.20derive.20deserialize.20to.20capture.20unknown.20enum.20.2E.2E.2E.html#243420536">(Jun 21 2021 at 18:05)</a>:</h4>
<p>You won't be able to do it with the derive macro, you'll need to manually implement <code>Deserialize</code> for your type, and add an <code>Unknown</code> variant to capture them.</p>



<a name="243420654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Getting%20serde%20derive%20deserialize%20to%20capture%20unknown%20enum%20.../near/243420654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David McGillicuddy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Getting.20serde.20derive.20deserialize.20to.20capture.20unknown.20enum.20.2E.2E.2E.html#243420654">(Jun 21 2021 at 18:06)</a>:</h4>
<p>Okay, that's fair enough. I feel like I've spent more time trying to contort my use case into derive than it would have taken for me to bite the bullet earlier :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>